<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$orderid=intval($_RQ['orderid']);
	if(!$orderid){
		exi('请选择订单！','error');
	}
	$this->session('order_id',$orderid);
	$peiset=$this->pdo_get('peiset',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$orderid));
	$peiset['address']=json_decode($peiset['address'],true);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
	$buymid=$order['mid'];
	//用户地址
	$order['address']=json_decode($order['address'],true);
	if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
		$address=$order['address'];
	}else{
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
	}
	if(!is_array($address['pcd'])){
		$address['pcd']=json_decode($address['pcd'],true);
	}
	$return['address']=$address;
	$return['peiset']=$peiset?:array('days'=>'','order_id'=>$orderid,'mid'=>$buymid);
	$return['peiset']['goods']=json_decode($return['peiset']['goods'],true);
	$return['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderid),array('istaocan','goodsid','optionid','orderid','price','total','goodsnum'));
	foreach($return['goods'] as $k=>&$g){
		$g['numset']=$return['peiset']['goods'][$k]['numset']?:array(
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
			array('num'=>1),
		);
		$goods=pdo_get('jy_weishop_goods',array('id'=>$g['goodsid']));
		$api_data=json_decode($goods['api_data'],true);
		$g['leixings']=$api_data['leixings']?:array();
		$g['unit']=$goods['unit'];
		$g['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$g['goodsid']),'weight');
		if($g['optionid']){
			$options=pdo_get('jy_weishop_goods_option',array('id'=>$g['optionid']),array('title','specs'));
			if($g['istaocan']==1){
					$specs_list=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'specs'=>''));
					$selespename = $specs_list[0]['title'];
			}else{
					$specs=$options['specs'];
					$g['price']=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'id'=>$g['optionid']),'price');
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
					}
					$optiontitle=implode("，",$specs);
					$selespename = $optiontitle;
			}
			$g['optiontitle']=$g['selespename']= $selespename;			
		}
		
		$g['goods_title']=$goods['title'];
		$g['issendnum']=$this->model('order')->get_goodsnum($g['orderid'],$g['goodsid']);
		$g['lastnum']=$g['goodsnum']-$g['issendnum'];
	}
	$return['params']=array(
		'nowmonth'=>date('Y-m'),
		'work_weekarray'=>array('日','一','二','三','四','五','六'),
	);
	$return['currentyear'] =$_RQ['year']?:date('Y');
	$return['currentmonth'] = $_RQ['month']?:date('m');
	if($_SESSION['onemerchid']){
		$return['yuesetting']=$this->plugin_setting('yuesetting'.$_SESSION['onemerchid']);
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi'.$_SESSION['onemerchid']);
	}else{
		$return['yuesetting']=$this->plugin_setting('yuesetting');
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi');
	}
	$return['gai_peihour']=$this->peisetting['gai_peihour'];
	set_html('title','设置配送');
	exi($return);
}
if($_RQ['op']=='goods'){
	$orderid=intval($_RQ['orderid']);
	if(!$orderid){
		exi('请选择订单！','error');
	}
	$peiset=$this->pdo_get('peiset',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$orderid));
	$buymid=pdo_getcolumn('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid),'mid');
	$return['peiset']=$peiset?:array('days'=>'','order_id'=>$orderid,'mid'=>$buymid);
	$return['peiset']['goods']=$return['peiset']['goods']?json_decode($return['peiset']['goods'],true):array('k'=>0);
	
	$return['params']=array(
		'nowmonth'=>date('Y-m'),
		'work_weekarray'=>array('日','一','二','三','四','五','六'),
	);
	if($_SESSION['onemerchid']){
		$return['yuesetting']=$this->plugin_setting('yuesetting'.$_SESSION['onemerchid']);
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi'.$_SESSION['onemerchid']);
	}else{
		$return['yuesetting']=$this->plugin_setting('yuesetting');
		$return['yuyuexieyi']=$this->plugin_setting('yuyuexieyi');
	}
	set_html('title','设置配送商品');
	exi($return);
}
if($_RQ['op']=='get_work_time'){
	$daytime=date('Y-m-d');
	if($_SESSION['onemerchid']){
		$yuesetting=$this->plugin_setting('yuesetting'.$_SESSION['onemerchid']);
	}else{
		$yuesetting=$this->plugin_setting('yuesetting');
	}
	$send1_timebox=$yuesetting['send1_timebox'];
	$key0=strtotime($daytime.' 12:00');
	$key1=strtotime($daytime.' 18:00');
	$key2=strtotime($daytime.' 24:00');
	$times0=array();
	$times1=array();
	$times2=array();
	foreach($send1_timebox as &$sv){
		$sv['key']=strtotime($daytime.' '.$sv['times']['start']);
		if($sv['key']<$key0){
			$times0[] = array(
				'start' => $sv['times']['start'],
				'end' => $sv['times']['end'],
				'timestamp' => $sv['key'],
				'timev' =>$sv['times']['start'].'~'.$sv['times']['end'],
			);
		}
		if($sv['key']>=$key0 && $sv['key']<$key1){
			$times1[] = array(
				'start' => $sv['times']['start'],
				'end' => $sv['times']['end'],
				'timestamp' => $sv['key'],
				'timev' =>$sv['times']['start'].'~'.$sv['times']['end'],
			);
		}
		if($sv['key']>=$key1){
			$times2[] = array(
				'start' => $sv['times']['start'],
				'end' => $sv['times']['end'],
				'timestamp' => $sv['key'],
				'timev' =>$sv['times']['start'].'~'.$sv['times']['end'],
			);
		}
	}
	$work_time=str_array($store['work_time']);
	//门店营业时间
	//设置日期
	$data=array();
	$weekarray=array('日','一','二','三','四','五','六'); //先定义一个数组
	$weekid=date("w");
	$newweekarray=array();
	for($i=1;$i<8;$i++){
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))));
		$newweekarray[]=$weekarray[$weekid];
	}
	for($i=1;$i<15;$i++){
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))));
		if(count($work_time)){
			$iswork=strpos($store['work_time'],';'.$weekid.';')?1:0;
		}else{
			$iswork=1;
		}
		$data[]=array(
			'riqi'=>date("Y-m-d",strtotime("+".$i." day")),
			'day'=>date("d",strtotime("+".$i." day")),
		);
	}
	$h=date('H');
	if($h>22){
		$nowkey=3;
	}elseif($h>18){
		$nowkey=2;
	}elseif($h>12){
		$nowkey=1;
	}else{
		$nowkey=0;
	}
	$return=array(
		'days'=>$data,
		'nowtime'=>time(),
		'nowriqi'=>$data[0]['riqi'],
		'nowkey'=>0,
		'hours0'=>$times0,
		'hours1'=>$times1,
		'hours2'=>$times2,
		'weekarray'=>$newweekarray
	);
	exi($return);
}
if($_RQ['op']=='time_days'){
	$order_id=$this->session('order_id');
		$year =$_RQ['year']?:date('Y');
		$month = $_RQ['month']?:date('m');
		$day = date("t",strtotime($year.'-'.$month));
		//$day = cal_days_in_month(CAL_GREGORIAN, $month,$year);
		$calendar = array();
		$i = 1;
		while ($i <= $day) {
				$key=$i;
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date=$year . '-' . $month . '-' . $i;
				if(strtotime($date)<time()){
					$islast=1;
				}else{
					$islast=0;
				}
				//商品
				$sendlog=pdo_get('jy_peiorder_sendlog',array('day'=>$date,'uniacid'=>$_SESSION['uniacid'],'order_id'=>$order_id));
				$goods=$sendlog['goods']?json_decode($sendlog['goods'],true):array();
				foreach($goods as &$gv){
					$gv['title']=cfc('tools')->substr($gv['title'],0,5);
				}
				$calendar[] = array(
					'i'=>$i,
					'day'=>$date,
					'islast'=>$islast,
					'goods'=>$goods
				);
				++$i;
		}
		$return['calendar'] = $calendar;
		$weekday=$year . '-' . $month . '-01';
		$return['week'] = date('w', strtotime($weekday));
		$return['kongday']=array();
		for($i=0;$i<$return['week'];$i++){
			$return['kongday'][]=$i;
		}
		
		$years = array();
		$i = 0;
		while ($i <= 10) {
			$years[] = $year + $i;
			++$i;
		}
		$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
		$return['months']=$months;
		$return['years']=$years;
		$return['year']=$year;
		$return['month']=$month;
		exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	$data=array(
		'set_mid'=>$_SESSION['uid'],
		'days'=>trim($post['days']),
		'times'=>trim($post['times']),
		'address'=>ijson_encode($post['address']),
	);
	if($post['id']){
		$data['uptime']=time();
		$this->update('peiset',$data,array('id'=>$post['id']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$post['mid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['order_id']=$post['order_id'];
		$data['createtime']=time();
		$this->insert('peiset',$data);
	}
	exi('提交成功','','goback');
}
if($_RQ['op']=='postgoods'){
	$order_id=$this->session('order_id');
	$post=get_postdata();
	$goods=$post;
	foreach($goods as $k=>$gv){
		if($gv['num']){
			//
		}
	}
	$buymid=pdo_getcolumn('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id),'mid');
	$data=array(
		'set_mid'=>$_SESSION['uid'],
		'goods'=>ijson_encode($goods),
	);
	$peiset=$this->pdo_get('peiset',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order_id));
	if($peiset['id']){
		$data['uptime']=time();
		$this->update('peiset',$data,array('id'=>$peiset['id']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$buymid;
		$data['merchid']=$_SESSION['onemerchid'];
		$data['order_id']=$order_id;
		$data['createtime']=time();
		$this->insert('peiset',$data);
	}
	$this->model('order')->upsendlog($goods,$order_id);
	exi('提交成功');
}